package com.ewhale.feitengguest.ui.user.mine.adapter;

import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.MyTaskDto;
import com.simga.library.adapter.recyclerview.BaseViewHolder;
import com.simga.library.adapter.recyclerview.MutiRecyclerAdapter;
import com.simga.library.utils.DateUtil;
import com.simga.library.utils.StringUtil;
import com.simga.library.utils.glide.GlideUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class MyTaskAdapter extends MutiRecyclerAdapter<MyTaskDto> {

    private SparseArray<MyTime> mMyTimeList;

    public MyTaskAdapter(List<MyTaskDto> data) {
        super(data,R.layout.item_mytask);
        mMyTimeList = new SparseArray<>();
    }

    @Override
    protected BaseViewHolder<MyTaskDto> getViewHolder() {
        return new ViewHolder(view);
    }

    class ViewHolder extends BaseViewHolder<MyTaskDto> {
        @BindView(R.id.iv_cover)
        ImageView mIvCover;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_times)
        TextView mTvTimes;
        @BindView(R.id.tv_price)
        TextView mTvPrice;
        @BindView(R.id.tv_status)
        TextView mTvStatus;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void build(MyTaskDto object, int position) {
            GlideUtil.loadPicture(object.getImg(), mIvCover, R.drawable.default_image);
            mTvTitle.setText(object.getTitle());
            mTvPrice.setText("￥" + StringUtil.to2Decimal(StringUtil.to2Double(object.getUnitPrice())));
            switch (object.getStatus()){
                case 0:
                    mTvStatus.setText("待提交");
                    break;
                case 1:
                    mTvStatus.setText("审核中");
                    break;
                case 2:
                    mTvStatus.setText("审核不通过");
                    break;
                case 3:
                    mTvStatus.setText("审核通过");
                    break;
                case 4:
                    mTvStatus.setText("过期关闭");
                    break;
            }
            if (object.getStatus() == 0) {
                mTvTimes.setVisibility(View.VISIBLE);
                if (object.getEndTimeStamp() > 0) {
                    MyTime timer = mMyTimeList.get(mTvTimes.hashCode());
                    if (timer != null) {
                        //将复用的倒计时清除
                        timer.cancel();
                    }
                    long countDown = (object.getEndTimeStamp() * 1000 - new Date().getTime()) / 1000;
                    timer = new MyTime(countDown * 1000,1000,mTvTimes);
                    timer.start();
                    mMyTimeList.put(mTvTimes.hashCode(), timer);
                } else {
                    mTvTimes.setText("过期关闭");
                }
            } else {
                mTvTimes.setVisibility(View.GONE);
            }
        }
    }

    private class MyTime extends CountDownTimer {
        private TextView tvOverTime;

        public MyTime(long millisInFuture, long countDownInterval, TextView tvOverTime) {
            super(millisInFuture, countDownInterval);
            this.tvOverTime = tvOverTime;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvOverTime.setText("剩余提交时间  " + DateUtil.formatSeconds(millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            tvOverTime.setText("已结束");
        }
    }
    /**
     * 清空当前 CountTimeDown 资源
     */
    public void cancelAllTimers() {
        if (mMyTimeList == null) {
            return;
        }
        for (int i = 0, length = mMyTimeList.size(); i < length; i++) {
            CountDownTimer cdt = mMyTimeList.get(mMyTimeList.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }
}
